<?php
class AuthorController extends My_Controller_Action{
    protected $_author;
    public function init()
    {
        $this->_author=new Default_Model_Author;
        $this->loadTemplates("admin");
        $this->_helper->getHelper("FlashMessenger");
    }
    public function listAction()
    {
         $this->view->headScript()->appendFile($this->view->baseUrl()."/public/js/customjs/author.js");
        $mauthor=new Default_Model_Author;
        $this->view->authors=$mauthor->listAuthor();
        
    }
    public function addAction()
    {
         $this->view->headScript()->appendFile($this->view->baseUrl()."/public/js/customjs/author.js");
        $this->view->headScript()->appendFile($this->view->baseUrl()."/public/js/ckeditor/ckeditor.js");
        $frm=new Default_Form_author;
        $frm->setAction($this->view->baseUrl("author/add"));
        if($this->_request->isPost())
        {
            $data=$this->_request->getPost();
            if($frm->isValid($data))
            {
                $insert_data=array();
                $insert_data["au_name"]= htmlentities($data["au_name"]);
                $insert_data["au_age"]=htmlentities($data["au_age"]);
                $insert_data["au_address"]=htmlentities($data["au_address"]);
                $insert_data["au_information"]=htmlentities($data["au_information"]);
                $id=$this->_author->insert($insert_data);
                if($id != null)
                {
                    $this->_helper->FlashMessenger(array('mess'=> 'Insert book author successfully'));
                    $this->_helper->redirector->gotoUrlAndExit('author/detail/author/'.$id);
                }
            }
            else
            {
                $frm->populate($data);
            }
        }
        $this->view->form=$frm;
    }
    public function editAction()
    {
        $this->view->headScript()->appendFile($this->view->baseUrl()."/public/js/customjs/author.js");
        $this->view->headScript()->appendFile($this->view->baseUrl()."/public/js/ckeditor/ckeditor.js");
        $id=$this->_request->getParam('author');
        if($id == null )
        {
            $this->_helper->redirector->gotoUrlAndExit('author/list');
        }
        $this->view->headTitle("Edit Author");
        $author=$this->_author->listAuthor(false,$id);
        $editFrm=new Default_Form_author;
        if($this->_request->isPost())
        {
            $edit_data=$this->_request->getPost();
            if($editFrm->isValid($edit_data))
            {
                $author["au_name"]=htmlentities($edit_data["au_name"]);
                $author["au_age"]=htmlentities($edit_data["au_age"]);
                $author["au_address"]=htmlentities($edit_data["au_address"]);
                $author["au_information"]=htmlentities($edit_data["au_information"]);
              //  Zend_Debug::dump($author);
               // die();
               if($this->_author->update($author->toArray(),"au_id =".$id))
               {
                    $this->_helper->redirector->gotoUrlAndExit('author/detail/author/'.$id);
               }
               else
               {
                $editFrm->populate($edit_data);
               }
                
                
                
            }else
            {
                $editFrm->populate($edit_data);
            }
        }
        $editFrm->au_name->setValue($author["au_name"]);
        $editFrm->au_age->setValue($author["au_age"]);
        $editFrm->au_address->setValue($author["au_address"]);
        $editFrm->au_information->setValue($author["au_information"]);
        $this->view->frm=$editFrm;
    }
    public function deleteAction()
    {
        $id=$this->_request->getParam('author');
        if($id != null )
        {
            $this->_author->delete_author($id);
        }
        $this->_helper->redirector->gotoUrlAndExit('author/list');
    }
    public function detailAction()
    {
        $id=$this->_request->getParam("author");
        $author=$this->_author->listAuthor(false,$id);
        $this->view->author=$author;   
    }
    
    
}